package com.bgIntlgn.client.entity.statement;

import java.util.Date;

import yuhuibear.poat.annotations.action.AutoGenerateValue;
import yuhuibear.poat.annotations.relation.Field;
import yuhuibear.poat.annotations.relation.Table;
import yuhuibear.poat.object.bean.Bean;

/**
 * 单据的序列号
 * 
 * @author yuhui
 * 
 */
@Table(name = "statement_serials", constrain = "")
public class SerialDefine implements Bean {

	@AutoGenerateValue(skipOnNull = true, sqlToGet = "select currval('statement_serials_id_seq')")
	@Field(name = "id", type = "bigserial", constrain = "primary key ")
	private Long id;

	/**
	 * 报表的名称, 对应于表的实体的数据库名。
	 */
	@Field(name = "name", type = "varchar(80)", constrain = "unique")
	private String name;

	@Field(name = "prefix", type = "varchar(80)", constrain = "")
	private String prefix;

	@Field(name = "infix", type = "varchar(80)", constrain = "")
	private String infix;

	@Field(name = "suffix", type = "varchar(80)", constrain = "")
	private String suffix;

	@Field(name = "blank_character", type = "varchar(80)", constrain = "")
	private String blankCharacter;

	@Field(name = "date_blank_character", type = "varchar(80)", constrain = "")
	private String dateBlankCharacter;

	/**
	 * 流水码的长度
	 */
	@Field(name = "length", type = "int", constrain = "default 3")
	private Integer length;

	/**
	 * 是否为6为日期. 可选为4， 6， 8
	 */
	@Field(name = "date_length", type = "int", constrain = "default 4")
	private Integer dateLength;

	/**
	 * 获取序列号。
	 * 
	 * @param serialNumber
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public String nextSerial(int serialNumber) {
		String serial = "";
		if (prefix != null) serial = serial + prefix;
		if (blankCharacter != null) serial = serial + blankCharacter;

		Date d = new Date();
		switch (dateLength) {
			case 6: {
				serial = serial + (d.getYear() + "").substring(2);
				serial = serial + (dateBlankCharacter == null ? "" : dateBlankCharacter);
				serial = serial
						+ (d.getMonth() < 10 ? "0" + (d.getMonth() + 1) : (d.getMonth() + 1))
						+ (dateBlankCharacter == null ? "" : dateBlankCharacter);
				serial = serial + (d.getDay() < 10 ? "0" + (d.getDay() + 1) : (d.getDay() + 1));
				if (blankCharacter != null) serial = serial + blankCharacter;
				break;
			}
			case 8: {
				serial = serial + (d.getYear() + "");
				serial = serial + (dateBlankCharacter == null ? "" : dateBlankCharacter);
				serial = serial
						+ (d.getMonth() < 10 ? "0" + (d.getMonth() + 1) : (d.getMonth() + 1))
						+ (dateBlankCharacter == null ? "" : dateBlankCharacter);
				serial = serial + (d.getDay() < 10 ? "0" + (d.getDay() + 1) : (d.getDay() + 1));
				if (blankCharacter != null) serial = serial + blankCharacter;
				break;
			}
			case 4: {
				serial = serial
						+ (d.getMonth() < 10 ? "0" + (d.getMonth() + 1) : (d.getMonth() + 1))
						+ (dateBlankCharacter == null ? "" : dateBlankCharacter);
				serial = serial + (d.getDay() < 10 ? "0" + (d.getDay() + 1) : (d.getDay() + 1));
				if (blankCharacter != null) serial = serial + blankCharacter;
				break;
			}
		}

		if (infix != null) serial = serial + infix;
		if (blankCharacter != null) serial = serial + blankCharacter;

		for (int i = (serialNumber + "").length(); i < length; i++)
			serial = serial + "0";
		serial = serial + serialNumber;
		if (blankCharacter != null && suffix != null) serial = serial + blankCharacter + suffix;
		return serial;
	}

	public Integer getLength() {
		return this.length;
	}

	public void setLength(Integer length) {
		this.length = length;
	}

	public Long getId() {
		return this.id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPrefix() {
		return this.prefix;
	}

	public void setPrefix(String prefix) {
		this.prefix = prefix;
	}

	public String getInfix() {
		return this.infix;
	}

	public void setInfix(String infix) {
		this.infix = infix;
	}

	public String getSuffix() {
		return this.suffix;
	}

	public void setSuffix(String suffix) {
		this.suffix = suffix;
	}

	public String getBlankCharacter() {
		return this.blankCharacter;
	}

	public void setBlankCharacter(String blankCharacter) {
		this.blankCharacter = blankCharacter;
	}

	public String getDateBlankCharacter() {
		return this.dateBlankCharacter;
	}

	public void setDateBlankCharacter(String dateBlankCharacter) {
		this.dateBlankCharacter = dateBlankCharacter;
	}

	public Integer getDateLength() {
		return dateLength;
	}

	public void setDateLength(Integer dateLength) {
		this.dateLength = dateLength;
	}

}
